package com.zr.inquiry.valid;

import com.zr.inquiry.partner.mapper.PartnerMapper;
import com.zr.inquiry.partner.model.pojo.Partner;
import com.zr.inquiry.spare.mapper.SpareMapper;
import com.zr.inquiry.spare.model.pojo.InquirySpareDetailsVo;
import com.zr.inquiry.total.mapper.InquiryMapper;
import com.zr.inquiry.total.mapper.LegalPersonMapper;
import com.zr.inquiry.total.model.pojo.LegalPerson;
import com.zr.inquiry.total.model.pojo.SpareVendor;
import com.zr.share.config.ErrorMassage;
import com.zr.share.enums.InquiryCurrencyEnum;
import com.zr.share.enums.InquiryEnum;
import com.zr.utils.result.ResultBuilderUtil;
import com.zr.utils.result.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @ClassName InquiryEditValid
 * @Description TODO 用于备件新增和修改的表单验证
 * @Author Jiakui_Ma
 * @Date 2019/11/15 16:56
 */
@Component
public class InquiryEditValid {

    @Autowired
    private PartnerMapper partnerMapper;
    @Autowired
    private SpareMapper spareMapper;
    @Autowired
    private InquiryMapper inquiryMapper;
    @Autowired
    private LegalPersonMapper legalPersonMapper;

    /*
    * @Author 马家奎
    * @Description //TODO 验证法人是否存在 * @Date
    * @Param Integer legalPersonId 法人ID
    * @return ResultUtil
    **/
    public ResultUtil validateLegalPersonExist(Integer currencyCode,Integer type,Integer legalPersonId) {
        //验证币别是否存在
        if (StringUtils.isEmpty(InquiryCurrencyEnum.getName(currencyCode))){
            return ResultBuilderUtil.error(ErrorMassage.INQUIRY_CURRENCY_NOT_EXIST);
        }
        //验证询价类型是否存在
        if (StringUtils.isEmpty(InquiryEnum.getName(type))){
            return ResultBuilderUtil.error(ErrorMassage.INQUIRY_TYPE_NOT_EXIST);
        }
        //验证法人是否存在
        LegalPerson legalPerson = legalPersonMapper.queryLegalPersonById(legalPersonId);
        if (legalPerson == null) {
            return ResultBuilderUtil.error(ErrorMassage.LEGAL_PERSON_NOT_EXIST);
        }else {
            return ResultBuilderUtil.success(null);
        }
    }
    /*
    * @Author 马家奎
    * @Description //TODO 开始日期是否在结束日期之前 * @Date
    * @Param Date startTime, Date endTime
    * @return
    **/
    public ResultUtil validateTimeIsBefore(Date startTime, Date endTime) {
        if (!startTime.before(endTime)) {
            return ResultBuilderUtil.error(ErrorMassage.START_TIME_MUST_PRECEDE_END_TIME);
        }else {
            return ResultBuilderUtil.success(null);
        }
    }
    /*
    * @Author 马家奎
    * @Description //TODO 验证备件信息不能重复并且必须存在 * @Date
    * @Param List<InquirySpareDetailsVo> inquiryDetailsList
    * @return
    **/
    public ResultUtil validateSpareIsRepeatedAndIsExist(List<InquirySpareDetailsVo> inquirySpares) {
        /*************************************验重***************************************************/
        //遍历所有的备件将备件编码添加到集合oldSpareCodes中
        List<String> oldSpareCodes = new ArrayList<>();
        inquirySpares.forEach(inquirySpare -> oldSpareCodes.add(inquirySpare.getSpareCoding()));
        //去重
        List<String> newSpareCodes = new ArrayList<>();
        List<String> newReSpareCodes = new ArrayList<>();
        oldSpareCodes.forEach(spareCode->{
            if (!newSpareCodes.contains(spareCode)) {
                newSpareCodes.add(spareCode);
            }else {
                newReSpareCodes.add(spareCode);
            }
        });
        if (!newReSpareCodes.isEmpty()) {
            return ResultBuilderUtil.error(ErrorMassage.SPARE_REPETITION +newReSpareCodes);
        }
        /**************************************是否存在**************************************************/
        List<String> parSpareCodes = new ArrayList<>();
        //拿到前台所有备件编码
        inquirySpares.forEach(spare -> parSpareCodes.add(spare.getSpareCoding()));
        //根据前台所有备件编码到数据库拿对应的备件编码
        List<String> dbSpareCodes =  spareMapper.queryBySpareCodes(parSpareCodes);
        parSpareCodes.removeAll(dbSpareCodes);
        if (!parSpareCodes.isEmpty()) {
            return ResultBuilderUtil.error(parSpareCodes+ErrorMassage.SPARE_NOT_EXIST);
        }
        return ResultBuilderUtil.success(null);
    }
    /*
    * @Author 马家奎
    * @Description //TODO 验证备件一定有生产他的供应商 * @Date
    * @Param List<InquirySpareDetailsVo> inquiryDetailsList
    * @return
    **/
    public ResultUtil validateSpareIsExistPartner(List<Partner> parSpareVendors,List<InquirySpareDetailsVo> parSpares ) {
        /****************************前台数据准备*********************************** */
        //拿出前台所有的供应商的编码
        List<String> parSpareVendorCodes = new ArrayList<>();
        parSpareVendors.forEach(partner -> parSpareVendorCodes.add(partner.getVendorCode()));
        //拿出前台所有的备件的编码
        List<String> parSpareCodes = new ArrayList<>();
        parSpares.forEach(spare -> parSpareCodes.add(spare.getSpareCoding()));
        /****************************数据库数据准备*********************************** */
        //拿到所有备件的所有供应商
        List<SpareVendor> dbSpareVendors  = spareMapper.queryPartnerBySpareCodes(parSpareCodes);
        /****************************根据前台的备件编码匹配对应的供应商***************** */
        for (String parSpareCode : parSpareCodes) {
            //拿到每个备件的所有供应商
            List<String> dbOneSpareVendorCodes = new ArrayList<>();
            dbSpareVendors.forEach(dbSpareVendor -> {
                if (dbSpareVendor.getSpareCoding().equals(parSpareCode)) {
                    dbOneSpareVendorCodes.add(dbSpareVendor.getVendorCode());
                }
            });
            //true不存在交集  false 代表存在交集
            if (Collections.disjoint(dbOneSpareVendorCodes,parSpareVendorCodes)) {
                return ResultBuilderUtil.error(parSpareCode+ErrorMassage.SPARE_NOT_EXIST_PARTNER+dbOneSpareVendorCodes);
            }
        }
        return ResultBuilderUtil.success(null);
    }
    /*
    * @Author 马家奎
    * @Description //TODO 验证供应商不能重复且必须存在 * @Date
    * @Param
    * @return
    **/
    public ResultUtil validatePartnerIsRepeatedAndIsExist(List<Partner> suppliers) {
        /***********************验证供应商不能重复********************************/
        List<String> oldPartnerCodes = new ArrayList<>();
        //拿到前台所有的供应商编码
        suppliers.forEach(partner -> oldPartnerCodes.add(partner.getVendorCode()));
        List<String> newPartnerCodes = new ArrayList<>();
        List<String> newRePartnerCodes = new ArrayList<>();
        //验重
        oldPartnerCodes.forEach(oldPartnerCode -> {
            if (newPartnerCodes.contains(oldPartnerCode)) {
                newRePartnerCodes.add(oldPartnerCode);
            }else {
                newPartnerCodes.add(oldPartnerCode);
            }
        });
        if (!newRePartnerCodes.isEmpty()) {
            return ResultBuilderUtil.error(ErrorMassage.PARTNER_REPETITION +newRePartnerCodes);
        }
        /***********************验证供应商必须存在********************************/
        List<String> parVendorCodes = new ArrayList<>();
        //拿到前台所有供应商编码
        suppliers.forEach(partner -> parVendorCodes.add(partner.getVendorCode()));
        //根据前台所有供应商编码到数据库拿对应的供应商编码
        List<String> dbVendorCodes =  partnerMapper.queryByPartnerCodes(parVendorCodes);
        parVendorCodes.removeAll(dbVendorCodes);
        if (!parVendorCodes.isEmpty()) {
            return ResultBuilderUtil.error(parVendorCodes+ErrorMassage.PARTNER_NOT_EXIST);
        }
        return ResultBuilderUtil.success(null);
    }
    /*
    * @Author 马家奎
    * @Description //TODO 验证供应商一定有可以生产的备件 * @Date
    * @Param
    * @return
    **/
    public ResultUtil validatePartnerIsExistSpare(List<Partner> parSpareVendors,List<InquirySpareDetailsVo> parSpares) {
        /****************************前台数据准备*********************************** */
        //拿出前台所有的供应商的编码
        List<String> parVendorCodes = new ArrayList<>();
        parSpareVendors.forEach(partner -> parVendorCodes.add(partner.getVendorCode()));
        //拿出前台所有的备件的编码
        List<String> parSpareCodes = new ArrayList<>();
        parSpares.forEach(spare -> parSpareCodes.add(spare.getSpareCoding()));
        /****************************数据库数据准备*********************************** */
        //拿到前台所有供应商可生产的所有备件
        List<SpareVendor> dbVendorSpares  = partnerMapper.querySpareCodeByPartorCodes(parVendorCodes);
        /****************************根据前台的备件编码匹配对应的供应商***************** */
        for (String parVendorCode : parVendorCodes) {
            //拿到每个供应商可生产的所有备件
            List<String> dbOneVendorSparesCodes = new ArrayList<>();
            dbVendorSpares.forEach(dbVendorSpare -> {
                if (dbVendorSpare.getVendorCode().equals(parVendorCode)) {
                    dbOneVendorSparesCodes.add(dbVendorSpare.getSpareCoding());
                }
            });
            //true不存在交集  false 代表存在交集
            if (Collections.disjoint(dbOneVendorSparesCodes,parSpareCodes)) {
                return ResultBuilderUtil.error(parVendorCode+ErrorMassage.PARTNER_NOT_EXIST_SPARE+dbOneVendorSparesCodes);
            }
        }
        return ResultBuilderUtil.success(null);
    }
}
